package mainProcesssor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import command.Command;
import command.CommandResolver;
import comparetor.BirthdayComparator;
import comparetor.IdComparator;
import exception.MyException;
import pojo.Customer;
import processor.DataProcessor;
import processor.impl.DataProcessorImpl;
import scanner.DataScanner;
import scanner.impl.DataScannerImpl;

public class MainProcessor {
	
	private Comparator<Customer> comparator;
	private DataProcessor dp;
	private DataScanner scanner;
	
	
	public void startUp() throws IOException,MyException{
		DataScanner scanner=new DataScannerImpl(new FileInputStream("data.txt"));
		List<Customer> list=scanner.scann();
		dp=new DataProcessorImpl(list, "line");
        ServerSocket  server =  new ServerSocket(8888);
		 Socket socket= server.accept();
		 InputStream in = socket.getInputStream();
		 OutputStream out = socket.getOutputStream();
		 out.write("Welcome to system!\r\n".getBytes());
		 while( socket.isConnected()){
			 BufferedReader br = new BufferedReader(new InputStreamReader(in));
			 String s = br.readLine();
			 if(s==null){
				 continue;
			 }
			 Command com =  CommandResolver.resoleCommand(s);
			 if(com.getOperationType()==null)
			 {
				 String mes = "It is not exist command '"+s+"' \r\n";
				  out.write(mes.getBytes());
				 continue;
			 }
			 switch(com.getOperationType()){
			  case "Lline":{
				     comparator = new IdComparator();
				     dp.SetFormat("line");
					StringBuilder sb=dp.process(comparator);
					out.write(sb.toString().getBytes());
					  break;
					
				  }
			  
			  case "Llineid": {
				  comparator = new IdComparator();
				  dp.SetFormat("line");
					StringBuilder sb=dp.process(comparator);
					out.write(sb.toString().getBytes());
				  break;
					
			  }			  
			  case "Llinebirth": {
				  comparator = new BirthdayComparator();
				  dp.SetFormat("line");
					StringBuilder sb=dp.process(comparator);
					out.write(sb.toString().getBytes());
				  break;
					
			  }
			  
			  case "Lxml": {
				  comparator = new IdComparator();
				  dp.SetFormat("xml");
					StringBuilder sb=dp.process(comparator);
					out.write(sb.toString().getBytes());
				  break;
					
			  }
			  
			  case "Lxmlid": {
				  comparator = new IdComparator();
				  dp.SetFormat("xml");
					StringBuilder sb=dp.process(comparator);
					out.write(sb.toString().getBytes());
				  break;
					
			  }
			  
			  case "Lxmlbirth": {
				  comparator = new BirthdayComparator();
				  dp.SetFormat("xml");
					StringBuilder sb=dp.process(comparator);
					out.write(sb.toString().getBytes());
				  break;
					
			  }
			  
			  case "Cxml": {
				  String name = (int)(Math.random()*100000000)+"txt";
				  OutputStream out1 = new FileOutputStream(new File(name));
				 comparator = new IdComparator();
				  dp.SetFormat("xml");
					StringBuilder sb=dp.process(comparator);
					out1.write(sb.toString().getBytes());
					out1.close();
					out.write("create file success!\r\n".getBytes());
				  break;
					
			  }
			  
			  case "Cxmlid": {
				  String name = (int)(Math.random()*100000000)+"txt";
				OutputStream out1 = new FileOutputStream(new File(name));
				comparator = new IdComparator();
				dp.SetFormat("xml");
				StringBuilder sb=dp.process(comparator);
				out1.write(sb.toString().getBytes());
				out1.close();
				out.write("create file success!\r\n".getBytes());
				  break;
					
			  }
			  
			  case "Cxmlbirth": {
				  String name = (int)(Math.random()*100000000)+"txt";
				   OutputStream out1 = new FileOutputStream(new File(name));
				   comparator = new BirthdayComparator();
					dp.SetFormat("xml");
					StringBuilder sb=dp.process(comparator);
					out1.write(sb.toString().getBytes());
					out1.close();
					out.write("create file success!\r\n".getBytes());
				  break;
					
			  }
			 
			 
			  default:
				 
				  break;
			 }
			 
		 }
		
	}
	
	
     
     
     
}
